package com.download.manager.beans;

import com.download.manager.DownLoadTask;
import com.download.manager.M3u8DownLoadManager;
import com.download.manager.interfaces.OnDownLoadTaskCallBack;


/**
 * 下载url与任务的bean
 */
public class LoadTaskBean {

    public String url;
    //下载状态值
    public int stateCode=0;
    //下载状态
    public String state;
    //下载进度
    public double progress=0;
    //可播放时长
    public double canPlayProgress=0;
    //总时长
    public double duration=100;
    //ts下载的片数
    public int tsProgress=0;
    //ts总片数
    public double tsSize=0;

    public DownLoadTask loadTask;

    public OnDownLoadTaskCallBack callBack=new OnDownLoadTaskCallBack() {
        @Override
        public void onDuration(String initialUrl, String key, double durationCB) {
            duration=durationCB;
            if(M3u8DownLoadManager.getInstance().getDownLoadTaskCallBack(url)!=null){
                M3u8DownLoadManager.getInstance().
                        getDownLoadTaskCallBack(url).onDuration(initialUrl, key, duration);
            }
        }

        @Override
        public void onLoadProgress(String initialUrl, String key, double progressCB, double durationCB) {
            progress=progressCB;
            duration=durationCB;
            if(M3u8DownLoadManager.getInstance().getDownLoadTaskCallBack(url)!=null){
                M3u8DownLoadManager.getInstance().
                        getDownLoadTaskCallBack(url).onLoadProgress(initialUrl, key, progress, duration);
            }
        }

        @Override
        public void onLoadTsProgress(String initialUrl, String key, int progressTS, double durationTS) {
            tsProgress=progressTS;
            tsSize=durationTS;
            if(M3u8DownLoadManager.getInstance().getDownLoadTaskCallBack(url)!=null){
                M3u8DownLoadManager.getInstance().
                        getDownLoadTaskCallBack(url).onLoadTsProgress(initialUrl, key, progressTS, durationTS);
            }
        }

        @Override
        public void onLoadTsDuration(String initialUrl, String key, int durationTS) {
            tsSize=durationTS;
            if(M3u8DownLoadManager.getInstance().getDownLoadTaskCallBack(url)!=null){
                M3u8DownLoadManager.getInstance().
                        getDownLoadTaskCallBack(url).onLoadTsDuration(initialUrl, key, durationTS);
            }
        }

        @Override
        public void onCanPlayProgress(String initialUrl, String key, double progress) {
            canPlayProgress=progress;
            if(M3u8DownLoadManager.getInstance().getDownLoadTaskCallBack(url)!=null){
                M3u8DownLoadManager.getInstance().getDownLoadTaskCallBack(url).
                        onCanPlayProgress(initialUrl, key, progress);
            }
        }

        @Override
        public void onM3u8SaveFile(String initialUrl, String key, String fileName, boolean isInitial) {
            if(M3u8DownLoadManager.getInstance().getDownLoadTaskCallBack(url)!=null){
                M3u8DownLoadManager.getInstance().
                        getDownLoadTaskCallBack(url).onM3u8SaveFile(initialUrl, key, fileName, isInitial);
            }
        }

        @Override
        public void onTSSaveFile(String initialUrl, String key, String fileName) {
            if(M3u8DownLoadManager.getInstance().getDownLoadTaskCallBack(url)!=null){
                M3u8DownLoadManager.getInstance().
                        getDownLoadTaskCallBack(url).onTSSaveFile(initialUrl, key, fileName);
            }
        }

        @Override
        public void onDownLoadFinish(String initialUrl, String key, boolean success) {
            if(M3u8DownLoadManager.getInstance().getDownLoadTaskCallBack(url)!=null){
                M3u8DownLoadManager.getInstance().
                        getDownLoadTaskCallBack(url).onDownLoadFinish(initialUrl, key, success);
            }
            loadNext(success);
        }

        @Override
        public void onDownLoadError(String initialUrl, String key, boolean success) {
            if(M3u8DownLoadManager.getInstance().getDownLoadTaskCallBack(url)!=null){
                M3u8DownLoadManager.getInstance().
                        getDownLoadTaskCallBack(url).onDownLoadError(initialUrl, key, success);
            }
            loadNext(success);
        }

        @Override
        public void onPauseDownLoad(String initialUrl, String key) {
            if(M3u8DownLoadManager.getInstance().getDownLoadTaskCallBack(url)!=null){
                M3u8DownLoadManager.getInstance().
                        getDownLoadTaskCallBack(url).onPauseDownLoad(initialUrl, key);
                M3u8DownLoadManager.getInstance().removeDownLoadTaskCallBack(initialUrl);
            }
            loadNext(false);
        }

        @Override
        public void onStartLoad(String initialUrl, String key) {
            if(M3u8DownLoadManager.getInstance().getDownLoadTaskCallBack(url)!=null){
                M3u8DownLoadManager.getInstance().
                        getDownLoadTaskCallBack(url).onStartLoad(initialUrl, key);
            }
        }
    };

    public LoadTaskBean initTaskBean(String url){
        this.url=url;
        loadTask=new DownLoadTask();
        loadTask.setInitialUrl(url,true,M3u8DownLoadManager.getInstance().SAVE_DIRS);
        loadTask.setChildTaskCallBackInfo(callBack);
        state="等待下载";
        stateCode=0;
        return this;
    }

    public void startTask(){
        loadTask.execute(url);
        state="正在下载";
        stateCode=1;
    }

    public void stopTask(){
        loadTask.stopTask();
        //任务停止之后，开启下一个任务
        M3u8DownLoadManager.getInstance().startNewTask(0,true);
    }

    public void loadNext(boolean success){
        //下载成功失败,开启下一个
        M3u8DownLoadManager.getInstance().refreshLoadTask(url);
    }

}
